<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="starttime">
      <el-input v-model="dataForm.starttime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endtime">
      <el-input v-model="dataForm.endtime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="goodprice">
      <el-input v-model="dataForm.goodprice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商品低价" prop="lowprice">
      <el-input v-model="dataForm.lowprice" placeholder="商品低价"></el-input>
    </el-form-item>
    <el-form-item label="需砍多少刀" prop="knife">
      <el-input v-model="dataForm.knife" placeholder="需砍多少刀"></el-input>
    </el-form-item>
    <el-form-item label="一刀最多砍多少钱" prop="knifemoney">
      <el-input v-model="dataForm.knifemoney" placeholder="一刀最多砍多少钱"></el-input>
    </el-form-item>
    <el-form-item label="互动描述" prop="content">
      <el-input v-model="dataForm.content" placeholder="互动描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodsName: '',
          starttime: '',
          endtime: '',
          goodprice: '',
          lowprice: '',
          knife: '',
          knifemoney: '',
          content: ''
        },
        dataRule: {
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          starttime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endtime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          goodprice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          lowprice: [
            { required: true, message: '商品低价不能为空', trigger: 'blur' }
          ],
          knife: [
            { required: true, message: '需砍多少刀不能为空', trigger: 'blur' }
          ],
          knifemoney: [
            { required: true, message: '一刀最多砍多少钱不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '互动描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargainactivity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsName = data.bargainActivity.goodsName
                this.dataForm.starttime = data.bargainActivity.starttime
                this.dataForm.endtime = data.bargainActivity.endtime
                this.dataForm.goodprice = data.bargainActivity.goodprice
                this.dataForm.lowprice = data.bargainActivity.lowprice
                this.dataForm.knife = data.bargainActivity.knife
                this.dataForm.knifemoney = data.bargainActivity.knifemoney
                this.dataForm.content = data.bargainActivity.content
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargainactivity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'goodsName': this.dataForm.goodsName,
                'starttime': this.dataForm.starttime,
                'endtime': this.dataForm.endtime,
                'goodprice': this.dataForm.goodprice,
                'lowprice': this.dataForm.lowprice,
                'knife': this.dataForm.knife,
                'knifemoney': this.dataForm.knifemoney,
                'content': this.dataForm.content
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
